/*
 * Copyright (c) 2008, Yifu Huang
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of SleekIce nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <copyright holder> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.sleekice.input;

import com.sleekice.core.SleekCore;
import com.sleekice.util.List;
import com.sleekice.util.ListNode;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/* handles keyboard input */
public class KeyboardInput extends KeyAdapter {
    public static final String ENG_LOWERCASE = "abcdefghijklmnopqrstuvwxyz";
    public static final String ENG_UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    /* missing " and \ */
    public static final String BASIC_SYMBOLS = " `-=[];',./~!@#$%^&*()_+{}|:<>?";
    public static final String NUMBERS = "1234567890";
    public static final String ENG_ALL = ENG_LOWERCASE + ENG_UPPERCASE;
    public static final String ALL = ENG_ALL+BASIC_SYMBOLS+NUMBERS;
    
    /* store all the keyboard triggers in a list */
    private List<KeyboardAction> triggers;
    
    /* store a list of released keys/characters */
    private List<Integer> pressedKeys;
    private List<Integer> releasedKeys;
    private List<Character> releasedChars;
    
    /* scratch vars */
    private ListNode<KeyboardAction> current;
    private KeyboardAction action;
    
    private String acceptedChars;
    
    /**
     * Constructor for a new KeyboardInput instance with given accepted
     * characters. Only these characters will show up in releasedChars.
     */ 
    public KeyboardInput() {
        acceptedChars = ALL;
        triggers = new List<KeyboardAction>();
        pressedKeys = new List<Integer>();
        releasedKeys = new List<Integer>();
        releasedChars = new List<Character>();
    }
    
    public void addTrigger(InputAction trigger, int keyCode) {
        triggers.add(new KeyboardAction(trigger, keyCode));
    }
    
    public void assignTo(SleekCore sleekcore) {
        sleekcore.addKeyListener(this);
    }
    
    @Override
    public void keyPressed(KeyEvent ke) {
        int key = ke.getKeyCode();
        current = triggers.getFirst();
        while (current != null) {
            action = current.getValue();
            if (action.keyCode == key) {
                action.trigger.pressKey();
            }
            current = current.next();
        }
        pressedKeys.add(new Integer(ke.getKeyCode()));
    }
    
    @Override
    public void keyReleased(KeyEvent ke) {
        int key = ke.getKeyCode();
        current = triggers.getFirst();
        while (current != null) {
            action = current.getValue();
            if (action.keyCode == key) {
                action.trigger.releaseKey();
            }
            current = current.next();
        }
        releasedKeys.add(new Integer(ke.getKeyCode()));
        /* if the key char is not in the acceptedChars, do not add to list */
        if (acceptedChars.indexOf(ke.getKeyChar()) != -1) {
            releasedChars.add(new Character(ke.getKeyChar()));
        }
    }
    
    /**
     * Every key press will be stored in a list. Calling this returns
     * the first key released and removes it from the list. If the key is held
     * pressed, it will continue to be added to the list in an accelerated
     * interval.
     */ 
    public Integer pollKeyPressed() {
        ListNode<Integer> node = pressedKeys.getFirst();
        if (node == null) {
            return null;
        }
        else {
            pressedKeys.deleteFirst();
            return node.getValue();
        }
    }
        
    /**
     * Every key release will be stored in a list. Calling this returns
     * the first key released and removes it from the list.
     */ 
    public Integer pollKeyReleased() {
        ListNode<Integer> node = releasedKeys.getFirst();
        if (node == null) {
            return null;
        }
        else {
            releasedKeys.deleteFirst();
            return node.getValue();
        }
    }
    
    /**
     * Every character representing one or more key releases will be stored
     * in a list. Calling this returns the first character and removes it from
     * the list.
     */ 
    public Character pollCharReleased() {
        ListNode<Character> node = releasedChars.getFirst();
        if (node == null) {
            return null;
        }
        else {
            releasedChars.deleteFirst();
            return node.getValue();
        }
    }
    
    public void setAcceptedChars(String acceptedChars) {
        this.acceptedChars = acceptedChars;
    }
    
    private class KeyboardAction {
        public InputAction trigger;
        public int keyCode;
        
        public KeyboardAction(InputAction trigger, int keyCode) {
            this.trigger = trigger;
            this.keyCode = keyCode;
        }
    }
}
